<template>
	<view class="address-select">
		<!-- 省市区选择器 -->
<!-- 		<u-select
			:default-value="addressCodeCity"
			title="请选择省市区"
			v-model="visible"
			mode="mutil-column-auto"
			confirm-color="#174a41"
			:safe-area-inset-bottom="true"
			:list="list"
			@confirm="onSelected"
		></u-select>
		<!-- 街道选择器 -->
<!-- 		<u-select
			v-if="listTwo"
			title="请选择街道"
			v-model="visibleTwo"
			mode="single-column"
			confirm-color="#174a41"
			:mask-close-able="false"
			:list="listTwo"
			@cancel="cancel"
			@confirm="confirm"
		></u-select> --> -->
	</view>
</template>

<script>
import cityData from '@/lib/picker.city.js';
// import { getAddressParent } from '@/api/storage/index.js';
export default {
	props: {
		add: {
			type: Boolean,
			default: false
		}
	},

	data() {
		return {
			addressCodeCity: [],
			cityIndex: '',
			visible: false,
			visibleTwo: false,
			list: cityData,
			listTwo: null, //街道
			result: [],
			districtCode: '' // 县级code
		};
	},

	methods: {
		// 点击城市
		show(e) {
			this.visible = true;
			if (e.addressCode) {
				this.addressCodeCity = [];
				for (let i = 0; i < 3; i++) {
					this.addressCodeCity.push(Number(e.addressCode[i]));
					// 这里因为老数据中含有nan/undefined 所有处理下
					if (!e.addressCode[i] || e.addressCode == NaN) {
						this.addressCodeCity = [];
						return;
					}
				}
				this.cityIndex = e.addressCode.join(',');
			}
			// this.$forceUpdate()
		},
		// 点击街道
		 showTwo(e) {
			console.log(e);
			if(e.districtCode === ''){
				return this.$message('请先选择所在城市');
			}
			this.getChildren(e.districtCode);
			this.districtCode = e.districtCode;
			// 如果数据异常跳出不往下执行
			for (let i = 0; i < 3; i++) {
				// 这里因为老数据中含有nan/undefined 所有处理下
				if (!e.addressCode[i] || e.addressCode == NaN) {
					this.addressCodeCity = [];
				}
			}
			if (e.addressCode) {
				this.addressCodeCity = e.addressCode;
				this.cityIndex = e.addressCode.join(',');
			} else {
				this.addressCodeCity = [];
			}
			this.visibleTwo = true;
		},
		hide() {
			tis.visible = false;
		},
		// 选择省市区
		onSelected(value) {
			this.visible = false;
			this.result = value;
			var district = value[value.length - 1];
			// 计算城市下标
			let list = this.list;
			let cityIndex = [];
			for (var i = 0; i < list.length; i++) {
				if (value[0].value === list[i].value) {
					cityIndex.push(i);
					// 找到省级之后计算市级 下标
					for (var k = 0; k < list[i].children.length; k++) {
						if (list[i].children && value[1].value === list[i].children[k].value) {
							cityIndex.push(k);
							// 找到市级下标之后计算县级下标
							for (var e = 0; e < list[i].children[k].children.length; e++) {
								if (list[i].children[k].children && value[2].value === list[i].children[k].children[e].value) {
									cityIndex.push(e);
									this.addressCodeCity = cityIndex;
									this.cityIndex = cityIndex.join(',');
									this.$emit('change', { value: this.result, addressCode: this.cityIndex });
									this.districtCode = district.value;
									this.getChildren(district.value);
									return;
								}
							}
						}
					}
				}
			}
		},
		// 获取街道
		 async getChildren(districtCode) {
			this.listTwo = [];
			// let list = [];
			if (districtCode) {
				this.districtCode = districtCode;
			}
			this.listTwo.push({
				value: '',
				label: '暂不选择'
			});
			await getAddressParent(districtCode ? districtCode : this.districtCode).then(res => {
				let rasultList = res.data;
				for (let i = 0; i < rasultList.length; i++) {
					this.listTwo.push({
						label: rasultList[i].name,
						value: rasultList[i].code
					});
				}
				// this.listTwo = list;
				this.visibleTwo = true;
			});
		},

		// 点击取消
		cancel() {
			// 如果选择完城市  或者  点击了街道
			if (this.result.length === 3 || this.result === 0) {
				this.result.push({
					value: '',
					label: '暂不选择'
				});
			} else {	//其他情况下则数据异常  归零处理
				this.result = [];
				this.result.push({
					value: '',
					label: '暂不选择'
				});
			}
			this.$emit('change', { value: this.result });
		},

		// 选择街道
		confirm(value) {
			// 如果选择完城市  或者  点击了街道
			if (this.result.length === 3 || this.result === 0) {
				this.result.push(value[0]);
			} else {	//其他情况下则数据异常  归零处理
				this.result = [];
				this.result.push(value[0]);
			}
			this.$emit('change', { value: this.result });
		}
	}
};
</script>

<style lang="scss"></style>
